<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台商品ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/goods/quanliGoods.class.php');
quanli_require('module/base/promotion/promotionGoods.class.php');

class ajax_goods {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品列表
                    $this->searchGoodsList();
                    break;

                case 2: //查询相同颜色下的商品信息（价格、促销活动、库存等）
                    $this->searchGoodsInfo();
                    break;

                case 3: //top 10
                    $this->searchTop();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    private function searchGoodsList() {

        global $G_SHOP;

        if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
            $page = 1;
        } else {
            $page = $_REQUEST['page'];
        }

        if (!isset($_REQUEST['tagID']) || !quanli_is_int($_REQUEST['tagID'])) {
            $tagID = 0;
        } else {
            $tagID = $_REQUEST['tagID'];
        }

        //城市ID
        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');
        if (!$city_id) {
            $city_array = $G_SHOP->getCache('city');                                        //读取缓存城市数据
            $city_id = $city_array[0]['id'];
        }

        //统计
        if ($tagID == 0) {
            //mod by allen.qiang 20170116 去除大分类限制 start
            //$count_sql = 'SELECT COUNT(*) AS goods_count FROM goods_list WHERE bigCategoryID = 1 AND useFlg > 0 AND cityID = ' . $city_id;
            $count_sql = 'SELECT COUNT(*) AS goods_count FROM goods_list WHERE useFlg > 0 AND cityID = ' . $city_id;

        } else {
            //$count_sql = 'SELECT COUNT(*) AS goods_count FROM goods_list gl
            //              INNER JOIN goods_tag_relation gtr ON gtr.tagID = ' . $tagID . ' AND gtr.postID = gl.postID
            //              WHERE bigCategoryID = 1 AND useFlg > 0 AND cityID = ' . $city_id;
            $count_sql = 'SELECT COUNT(*) AS goods_count FROM goods_list gl
                          INNER JOIN goods_tag_relation gtr ON gtr.tagID = ' . $tagID . ' AND gtr.postID = gl.postID
                          WHERE useFlg > 0 AND cityID = ' . $city_id;
            //mod by allen.qiang 20170116 去除大分类限制 end
        }

        $count_array =& $G_SHOP->DBCA->stmt_query($count_sql);
        $count = $count_array[0]->goods_count;
        if ($count == 0) {
            returnJsonData('没有商品数据', 404);
        }

        //查询
        if ($tagID == 0) {
            //mod by allen.qiang 20170116 去除大分类限制 start
            //$search_sql = 'SELECT * FROM goods_list gl WHERE bigCategoryID = 1 AND useFlg > 0 AND cityID = ' . $city_id;
            $search_sql = 'SELECT * FROM goods_list gl WHERE useFlg > 0 AND cityID = ' . $city_id;
        } else {
            //$search_sql = 'SELECT gl.* FROM goods_list gl
            //              INNER JOIN goods_tag_relation gtr ON gtr.tagID = ' . $tagID . ' AND gtr.postID = gl.postID
            //              WHERE bigCategoryID = 1 AND useFlg > 0 AND cityID = ' . $city_id;
            $search_sql = 'SELECT gl.* FROM goods_list gl
                          INNER JOIN goods_tag_relation gtr ON gtr.tagID = ' . $tagID . ' AND gtr.postID = gl.postID
                          WHERE useFlg > 0 AND cityID = ' . $city_id;
            //mod by allen.qiang 20170116 去除大分类限制 end
        }
        $search_sql .= ' ORDER BY gl.sortOrder ASC LIMIT ?,?';

        $goods_list_array =& $G_SHOP->DBCA->stmt_query($search_sql, 'ii', ($page - 1) * 6, 6);
        if (!$goods_list_array) {
            returnJsonData('没有商品数据', 404);
        }

        $goods_array = array();
        foreach($goods_list_array as &$item) {
            $goods_array[] = array(
                'wap_link' => set_goods_link( $item->uid, 2 ),
                'img_link' => set_goods_img_link( $item->goodsMainPic ),
                'frenchName' => $item->frenchName,
                'goodsName' => $item->goodsName,
                'useFlg' => $item->useFlg
            );
        }

        $result_array = array(
            'list' => $goods_array,
            'page_count' => ceil($count / 6)
        );

        returnJsonData('ok', 200, $result_array);

    }

    private function searchTop() {

        global $G_SHOP;

        //城市ID
        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');
        if (!$city_id) {
            $city_array = $G_SHOP->getCache('city');                                        //读取缓存城市数据
            $city_id = $city_array[0]['id'];
        }

        //查询
        $search_sql = 'SELECT * FROM goods_list WHERE bigCategoryID = 1 AND useFlg = 1 AND cityID = ' . $city_id;
        $search_sql .= ' ORDER BY saletotal ASC LIMIT 10';
        $goods_list_array =& $G_SHOP->DBCA->stmt_query($search_sql);
        if (!$goods_list_array) {
            returnJsonData('没有商品数据', 404);
        }

        $goods_array = array();
        foreach($goods_list_array as &$item) {
            $goods_array[] = array(
                'wap_link' => set_goods_link( $item->uid, 2 ),
                'img_link' => set_goods_img_link( $item->goodsMainPic ),
                'frenchName' => $item->frenchName,
                'goodsName' => $item->goodsName
            );
        }

        $result_array = array(
            'list' => $goods_array,
        );

        returnJsonData('ok', 200, $result_array);

    }

    /**
     * 查询相同颜色下的商品信息（价格、促销活动、库存等）
     */
    private function searchGoodsInfo() {

        global $G_SHOP;

        //三级网购号
        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
            returnJsonData('参数错误');
        }

        $postID = $_REQUEST['id'];

        $cache = $G_SHOP->sessionCache_get('goods_info_'.$postID, true);

        if($cache)
            returnJsonData('cache', 200, $cache);

        //城市ID
        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');
        if (!$city_id) {
            $city_array = $G_SHOP->getCache('city');                                        //读取缓存城市数据
            $city_id = $city_array[0]['id'];
        }

        quanli_require('module/base/promotion/promotionBill.class.php');

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        //实例化订单促销类
        $promotionBill = new promotionBill( $G_SHOP->DBCA );

        //实例化订单促销类
        $promotionGoods = new promotionGoods( $G_SHOP->DBCA );


        /**
         * ============================
         * 查询商品信息
         * ============================
         */
        $arg_array = array(
            'postID' => $postID,
        );
        $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
        if (!$goods_array) {
            returnJsonData('商品信息没有找到');
        }
        unset($goods_array['description']);
        /**
         * ============================
         * 查询商品信息
         * ============================
         */


        /**
         * ============================
         * 查询同款商品不同尺寸的商品
         * ============================
         */
        $goods_list_array = $quanliGoods->getGoodsSizeSpec( $goods_array['twoPostID'] );
        /**
         * ============================
         * 查询同款商品不同尺寸的商品
         * ============================
         */


        /**
         * ============================
         * 计算商品价格、积分等信息，查询商品出库活动，查询商品库存
         * ============================
         */
        if ($goods_list_array) {
            foreach($goods_list_array as &$goods) {

                if ($goods['useFlg'] == 0) {

                    //根据会员等级计算商品价格
                    $goods['price'] = $goods['salePrice'];
                    $goods['promotion'] = null;
                    $goods['bill'] = null;

                } else {

                    //根据会员等级计算商品价格
                    $goods['price'] = $goods['salePrice'];

                    //查询商品促销活动
                    $goods['promotion'] = $promotionGoods->getGoodsPromotion( $postID );

                    //查询订单促销活动
                    $goods['bill'] = $promotionBill->getBillPromotion( $postID );

                }

            }
        }
        /**
         * ============================
         * 计算商品价格、积分等信息，查询商品出库活动，查询商品库存
         * ============================
         */


        /**
         * ============================
         * 统计商品评价数量
         * ============================
         */
        if ($G_SHOP->currentPCustID) {

            $search_sql = 'SELECT COUNT(*) AS comment_count FROM content_comments WHERE twoPostID = ? AND comment_status = 1 AND (display = 1 OR (pcustID = ? AND display = 0))';
            $comment_count_array = $G_SHOP->DBCA->stmt_query($search_sql, 'ii', $goods_array['twoPostID'], $G_SHOP->currentPCustID);

            $search_sql = 'SELECT COUNT(comment_item) AS item_count, cct.comment_item FROM (
                                SELECT comment_id FROM content_comments WHERE twoPostID = ? AND comment_status = 1 AND
                                (display = 1 OR (pcustID = ? AND display = 0))
                           ) AS T_comment
                           INNER JOIN content_comments_item cct ON cct.comment_id = T_comment.comment_id GROUP BY comment_item';
            $comment_item_count_array = $G_SHOP->DBCA->stmt_query($search_sql, 'ii', $goods_array['twoPostID'], $G_SHOP->currentPCustID);

        } else {

            $search_sql = 'SELECT COUNT(*) AS comment_count FROM content_comments WHERE twoPostID = ? AND comment_status = 1 AND display = 1';
            $comment_count_array = $G_SHOP->DBCA->stmt_query($search_sql, 'i', $goods_array['twoPostID']);

            $search_sql = 'SELECT COUNT(comment_item) AS item_count, cct.comment_item FROM (
                                SELECT comment_id FROM content_comments WHERE twoPostID = ? AND comment_status = 1 AND display = 1
                           ) AS T_comment
                           INNER JOIN content_comments_item cct ON cct.comment_id = T_comment.comment_id GROUP BY comment_item';
            $comment_item_count_array = $G_SHOP->DBCA->stmt_query($search_sql, 'i', $goods_array['twoPostID']);

        }



        /**
         * ============================
         * 统计商品评价数量
         * ============================
         */

        //返回的商品数据
        $data_array = array(
            'goodsList' => $goods_list_array,
            'comment_count' => $comment_count_array[0]->comment_count,
            'comment_item' => array()
        );

        if ($comment_item_count_array) {
            foreach($comment_item_count_array as &$item) {
                $data_array['comment_item'][$item->comment_item] = $item->item_count;
            }
        }

        $G_SHOP->sessionCache_set('goods_info_'.$postID,$data_array,0,true);

        returnJsonData('ok', 200, $data_array);

    }

}